<template>
  <div class="order">
    <!-- 搜索框 -->
    <el-row
      class="demo-autocomplete"
      style="margin-top: 20px; margin-bottom: 20px"
    >
      <el-col>
        <el-input
          @input="searchByNo(input1)"
          v-model="input1"
          style="width: 15%; margin-left: 15px"
          placeholder="请输入订单编号"
        ></el-input>
        <el-button
          plain
          type="info"
          style="margin-left: 10px"
          @click="searchByNo(input1)"
          >订单编号</el-button
        >
        <el-input
          @input="searchByName(input2)"
          v-model="input2"
          style="width: 15%; margin-left: 25px"
          placeholder="请输入姓名"
        ></el-input>
        <el-button
          plain
          type="info"
          style="margin-left: 10px"
          @click="searchByName(input2)"
          >收货人</el-button
        >
        <el-input
          v-model="input3"
          @input="searchByRegions(input3)"
          style="width: 15%; margin-left: 25px"
          placeholder="请输入城市"
        ></el-input>
        <el-button
          plain
          type="info"
          style="margin-left: 10px"
          @click="searchByRegions(input3)"
          >城市</el-button
        >
        <el-input
          v-model="input4"
          @input="searchByAddress(input4)"
          style="width: 15%; margin-left: 25px"
          placeholder="请输入详细地址"
        ></el-input>
        <el-button
          plain
          type="info"
          style="margin-left: 10px"
          @click="searchByAddress(input4)"
          >地址</el-button
        >
      </el-col>
    </el-row>
    <!-- 搜索结果展示 -->
    <el-table :data="selectOrder" v-show="flag" border style="width: 100%">
      <el-table-column prop="no" label="订单编号"> </el-table-column>
      <el-table-column prop="receiver" label="收货人"> </el-table-column>
      <el-table-column prop="regions" label="收货城市"> </el-table-column>
      <el-table-column prop="address" label="收货地址"> </el-table-column>
      <el-table-column prop="price" label="金额"> </el-table-column>
      <el-table-column prop="isPayed" label="支付状态">
        <template scope="scope">
          <el-switch
            v-model="scope.row.isPayed"
            active-color="#13ce66"
            inactive-color="#ff4949"
            @change="editIsPayed(scope.row._id, scope.row.isPayed)"
          >
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column prop="createdAt" label="订单创建时间"> </el-table-column>
      <el-table-column fixed="right" label="操作">
        <template slot-scope="scope">
          <!-- scope.row 获取当前这条数据 -->
          <!-- <el-button @click="editOrder(scope.row)" type="primary" size="small"
            >修改订单</el-button
          > -->
          <el-popconfirm
            title="确定关闭这个订单吗？"
            @onConfirm="delOrder(scope.row)"
          >
            <el-button type="danger" size="small" slot="reference"
              >关闭订单</el-button
            >
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>

    <!-- 订单列表  -->
    <el-table :data="tableData" v-show="!flag" border style="width: 100%">
      <el-table-column prop="no" label="订单编号"> </el-table-column>
      <el-table-column prop="receiver" label="收货人"> </el-table-column>
      <el-table-column prop="regions" label="收货城市"> </el-table-column>
      <el-table-column prop="address" label="收货地址"> </el-table-column>
      <el-table-column prop="price" label="金额"> </el-table-column>
      <el-table-column prop="isPayed" label="支付状态">
        <template scope="scope">
          <el-switch
            v-model="scope.row.isPayed"
            active-color="#13ce66"
            inactive-color="#ff4949"
            @change="editIsPayed(scope.row._id, scope.row.isPayed)"
          >
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column prop="createdAt" label="订单创建时间"> </el-table-column>
      <el-table-column fixed="right" label="操作">
        <template slot-scope="scope">
          <!-- scope.row 获取当前这条数据 -->
          <!-- <el-button @click="editOrder(scope.row)" type="primary" size="small"
            >修改订单</el-button
          > -->
          <el-popconfirm
            title="确定关闭这个订单吗？"
            @onConfirm="delOrder(scope.row)"
          >
            <el-button type="danger" size="small" slot="reference"
              >关闭订单</el-button
            >
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <el-pagination
      background
      style="margin-top: 20px; margin-bottom: 50px"
      layout="total, sizes, prev, pager, next, jumper"
      :total="totalCount"
      :page-size="PageSize"
      :page-sizes="PageSizes"
      :current-page="currentPage"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    >
    </el-pagination>

    <!-- 修改订单信息 -->
    <!-- <el-dialog title="修改订单信息" :visible.sync="dialogVisible" width="60%">
      <el-form ref="order1" :model="order" label-width="80px">
        <el-form-item label="订单编号">
          <el-input v-model="order.no" :disabled="true"></el-input>
        </el-form-item>
        <el-form-item label="收货人">
          <el-input v-model="order.receiver" :disabled="true"></el-input>
        </el-form-item>
        <el-form-item label="地址">
          <el-input v-model="order.address" :disabled="true"></el-input>
        </el-form-item>
        <el-form-item label="金额">
          <el-input v-model="order.price" :disabled="true"></el-input>
        </el-form-item>
        <el-form-item label="支付状态">
          <el-input v-model="order.isPayed"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="editSure">确 定</el-button>
      </span>
    </el-dialog> -->
  </div>
</template>

<script>
import {
  getOrders,
  xiugaiOrder,
  deleteOrder,
  getPerOrder,
} from "../../api/order";
import axios from "axios";
export default {
  name: "OrderList",
  data() {
    return {
      tableData: [],
      selectOrder: [],
      order: {},
      dialogVisible: false,
      currentPage: 1,
      totalCount: 0,
      PageSize: 10,
      PageSizes: [10, 20, 30],
      flag: false,
      id: "",
      input1: "",
      input2: "",
      input3: "",
      input4: "",
    };
  },
  computed: {
    searchInput1() {
      return this.input1;
    },
    searchInput2() {
      return this.input2;
    },
    searchInput3() {
      return this.input3;
    },
    searchInput4() {
      return this.input4;
    },
  },
  watch: {
    searchInput1(newVal, oldVal) {
      if (newVal === "") {
        this.flag = false;
        this.initProducts(); //加载商品列表
      }
    },
    searchInput2(newVal, oldVal) {
      if (newVal === "") {
        this.flag = false;
        this.initProducts(); //加载商品列表
      }
    },
    searchInput3(newVal, oldVal) {
      if (newVal === "") {
        this.flag = false;
        this.initProducts(); //加载商品列表
      }
    },
    searchInput4(newVal, oldVal) {
      if (newVal === "") {
        this.flag = false;
        this.initProducts(); //加载商品列表
      }
    },
  },
  filters: {},

  created() {
    this.initOrders();
  },

  methods: {
    //获取订单列表
    async initOrders(per, page) {
      const result = await getOrders(per, page);
      console.log("初始化商品", result);
      this.tableData = result.orders;
      // console.log(this.tableData);
      this.totalCount = result.totalCount;
    },

    //分页
    handleSizeChange(val) {
      // 改变每页显示的条数
      this.PageSize = val;
      // 点击每页显示的条数时，显示第一页
      this.initOrders(val, 1);
      // 注意：在改变每页显示的条数时，要将页码显示到第一页
      this.currentPage = 1;
    },
    // // 显示第几页
    handleCurrentChange(val) {
      // 改变默认的页数
      this.currentPage = val;
      // 切换页码时，要获取每页显示的条数
      this.initOrders(this.PageSize, val);
    },

    async editIsPayed(id, value) {
      //修改订单
      console.log(id, value);
      const result = await xiugaiOrder(id, {
        isPayed: value,
      });
      console.log("修改成功", result);
      this.order.isPayed = value; // 再次调用接口
    },

    async delOrder(order) {
      //删除订单
      const result = await deleteOrder(order._id);
      if (result.code === 20000) {
        console.log("删除成功", result);
        this.initOrders(); // 再次调用接口
      } else {
        console.log(result.error);
      }
    },

    //搜索订单
    //通过id搜索
    async searchOrder(id) {
      if (id) {
        const result = await getPerOrder(id);
        console.log(result);
        this.selectOrder.push(result);
        console.log(this.selectOrder);
        this.flag = true;
      } else {
        this.selectOrder = [];
        this.flag = false;
        this.initOrders();
      }
    },

    //通过订单编号搜索
    searchByNo(num) {
      this.tableData.forEach((item) => {
        if (item.no.includes(num)) {
          this.id = item._id;
          this.searchOrder(this.id);
        }
      });
    },

    //通过收货人搜索
    searchByName(nameStr) {
      this.tableData.forEach((item) => {
        // console.log(item.receiver);
        if (item.receiver.includes(nameStr)) {
          this.id = item._id;
          this.searchOrder(this.id);
        }
      });
    },
    //通过城市搜索
    searchByRegions(cityStr) {
      this.tableData.forEach((item) => {
        if (item.regions.includes(cityStr)) {
          this.id = item._id;
          this.searchOrder(this.id);
        }
      });
    },
    //通过地址搜索
    searchByAddress(strAddress) {
      this.tableData.forEach((item) => {
        if (item.address.includes(strAddress)) {
          this.id = item._id;
          this.searchOrder(this.id);
        }
      });
    },
  },
  mounted() {},
  components: {},
};
</script>
<style scoped></style>
